package com.atguigu.controller;

import com.atguigu.annotations.MethodExporter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Author: shenhao
 * @CreatTime: 2024/11/4 10:13
 * @Version: 1.0
 * 演示aop切面, 切入注解＋自定义注解实现对Controller里面的方法执行日志
 * 自定义注解+ aop切面 自定义控制层controller日志 **
 */
@RestController
@Slf4j
public class MethodExporterController {
    @MethodExporter
    @GetMapping("/method/list")
    public Map list(@RequestParam(value = "page",defaultValue = "1") int page,
                    @RequestParam(value = "rows",defaultValue = "10") int rows){
        Map<String,String> result = new HashMap<>();
        result.put("code","200");
        result.put("msg","success");
        try {
            TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000)+1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return result;
    }
    @MethodExporter
    @GetMapping(value = "/method/get")
    public Map get(){
        Map<String,String> result = new HashMap<>();
        result.put("code","404");
        result.put("msg","not found");
        try {
            TimeUnit.MILLISECONDS.sleep(new Random().nextInt(1000)+1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return result;
    }

    @GetMapping(value = "/method/update")
    public String update(){
        System.out.println("update method without @MethodExporter");
        return "ok update";
    }
}
